package com.xiaobo.framework.endpoint;

import com.xiaobo.framework.api.response.AuthenticationResponse;
import com.xiaobo.framework.common.exception.BizException;
import com.xiaobo.framework.service.SecretKeyAuth;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;


/**
 * @author: <a href="get_xiaobo@163.com">Josh_Xu</a>
 * @description:
 * @create: 2020-05-19 23:56
 **/
@RestController
@RequestMapping("/account")
@Slf4j
@Api(tags = "授权认证Api")
public class AcountEndpoint implements ApplicationContextAware {

    private ApplicationContext applicationContext;
    private SecretKeyAuth secretKeyAuth;


    @ApiOperation(value = "获取授权token", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, response = AuthenticationResponse.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器异常")
    })
    @GetMapping(value = "/v1/authentication", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> authentication(@ApiParam(value = "手机号") @RequestParam(value = "phone") String phone,
                                     @ApiParam(value = "密码") @RequestParam(value = "password") String password) {
        try {

        } catch (BizException ex) {
            log.error("消息错误", ex);
        }
        return ResponseEntity.ok().build();
    }


    @ApiOperation(value = "获取公钥", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, response = Object.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器异常")
    })
    @GetMapping(value = "/v1/get-public-key", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> publicKey() {
        try {
            String publicKey = secretKeyAuth.getPublicKey();
            HashMap<String, String> map = new HashMap<>();
            map.put("publicKey",publicKey);
            return ResponseEntity.ok(map);

        } catch (BizException | IOException ex) {
            log.error("消息错误", ex);
        }
        return ResponseEntity.ok().build();
    }

    @ApiOperation(value = "注册授权平台", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, response = Object.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 500, message = "服务器异常")
    })
    @PostMapping(value = "/v1/register", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<?> register(@ApiParam(value = "手机号") @RequestParam(value = "phone") String phone) {
        try {

        } catch (BizException ex) {
            log.error("消息错误", ex);
        }
        return ResponseEntity.ok().build();
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        SecretKeyAuth secretKeyAuth = (SecretKeyAuth) applicationContext.getBean("secretKeyAuth");
        this.secretKeyAuth = secretKeyAuth;
    }
}
